package reservation.flight;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import reservation.flight.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class NewUserActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.new_user);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
		
        Button b_registrar=(Button)findViewById(R.id.NU_Create_B);
        Button b_back=(Button)findViewById(R.id.NU_Back_B);
        final EditText nombre=(EditText)findViewById(R.id.NU_UserName_ET);
        final EditText usuario=(EditText)findViewById(R.id.NU_UserID_ET);
        final EditText contra=(EditText)findViewById(R.id.NU_User_Pass_ET);
        final TextView mensaje=(TextView)findViewById(R.id.NU_Error_TW);
        b_back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        
        b_registrar.setOnClickListener(new OnClickListener() {			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(nombre.length()>0&&usuario.length()>0&&contra.length()>0){
					Function fun=new Function();
					ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			        nameValuePairs.add(new BasicNameValuePair("opcion","confirmarusuario"));
			        nameValuePairs.add(new BasicNameValuePair("login",usuario.getText().toString()));
			        nameValuePairs.add(new BasicNameValuePair("pass",contra.getText().toString()));
			        nameValuePairs.add(new BasicNameValuePair("name",nombre.getText().toString()));
			        String result= fun.conexion(nameValuePairs);
				    if(fun.comprobarExistenciaUsuario(result)){
				    	mensaje.setText(getString(R.string.saving_user));
				    	nameValuePairs.add(new BasicNameValuePair("opcion","ingresausuario"));
				    	result= fun.conexion(nameValuePairs);
						if(fun.guardarUsuario(result)){			
							mensaje.setText(getString(R.string.user_create));	
						}
				    }
				}else{
					mensaje.setText(getString(R.string.empty_field));
				}
				
			}
		});
    }
}
